package org.msss.gamebasic;

import org.msss.resource.*;
import org.lwjgl.input.Keyboard;
import java.util.ArrayList;

/**
 * This class will hold the values of the view, gamemap, sprites, all that fun
 * stuff.
 * 
 * @author alanxoc3
 * 
 */
public final class Game {

	public static Room room;
	public static View view;

	private static ArrayList<Integer> keysDown;

	static {
		room = RoomDB.testRoom;
		view = new View();

		keysDown = new ArrayList<Integer>();
	}

	/**
	 * The game loop
	 */
	public static void loop() {
		input();
		move();
		collision();
		draw();
	}

	public static void input() {
		
		//Key pressed and released
		while (Keyboard.next()) {
			if (Keyboard.getEventKeyState()) {
				keysDown.add(Keyboard.getEventKey());
			} else {
				keysDown.remove((Integer) Keyboard.getEventKey());
			}
			
			for (int loop = 0; loop < room.spriteSize(); loop++) {
				if (Keyboard.getEventKeyState()) {
					room.getSprite(loop).pressed(Keyboard.getEventKey());
				} else {
					room.getSprite(loop).released(Keyboard.getEventKey());
				}
			}
		}
		
		//Key held down
		for (int loop = 0; loop < room.spriteSize(); loop++) {
			for (int downLoop = 0; downLoop < keysDown.size(); downLoop++) {
				room.getSprite(loop).down(keysDown.get(downLoop));
			}
		}
		
		//NOTE: if a key has just been pressed, it is also down!!!
	}

	/**
	 * move all game objects
	 */
	public static void move() {
		for (int loop = 0; loop < room.spriteSize(); loop++) {
			room.getSprite(loop).move();
		}
	}

	/**
	 * check for collisions
	 */
	public static void collision() {
		for (int loop = 0; loop < room.spriteSize(); loop++) {
			room.getSprite(loop).collision();
		}
	}

	/**
	 * draw everything to the frame
	 */
	public static void draw() {
		for (int loop = 0; loop < room.iconSize(); loop++) {
			room.getIcon(loop).draw();
		}

		for (int loop = 0; loop < room.spriteSize(); loop++) {
			room.getSprite(loop).draw();
		}
	}
}
